package com.client.controller;

import com.Emplyment.exception.BaseException;
import com.Emplyment.result.Result;
import com.Emplyment.vo.ResumeTemplateDownAndPreviewVo;
import com.Emplyment.vo.ResumeTemplateVo;
import com.client.service.ResumeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/template")
@Slf4j
public class TemplateController {

    @Autowired
    private ResumeTemplateService resumeTemplateService;

    //查询简历模板
//    @GetMapping("/getResumeTemplate")
//    public Result<List<ResumeTemplateVo>> getResumeTemplate() {
//        log.info("获取简历模板");
//        List<ResumeTemplateVo> resumeTemplate = resumeTemplateService.getResumeTemplate();
//        log.info("查询到的简历模板信息为：{}",resumeTemplate);
//        return Result.success(resumeTemplate) ;
//    }

//    private static final String FILE_DIRECTORY = "D:/就业/后端实习生.pdf"; // 本地文件目录
    @GetMapping("/preview")
    public ResponseEntity<Resource> previewFile(@RequestParam Integer templateId) throws IOException {
// 构建文件路径
//        Path filePath = Paths.get(FILE_DIRECTORY);
//        Resource resource = new UrlResource(filePath.toUri());
        log.info("文件Id：{}",templateId);



        // 检查文件是否存在
//        if (!resource.exists()) {
//            throw new RuntimeException("文件不存在: ");
//        }

        ResumeTemplateDownAndPreviewVo resumeTemplateDownAndPreviewVo = resumeTemplateService.ByIdGetResumeTemplate(templateId);
        byte[] file = resumeTemplateDownAndPreviewVo.getPreviewFile();
        // 转换为Resource
        Resource resource = new ByteArrayResource(file);
        String fileName = resumeTemplateDownAndPreviewVo.getName();

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(resource.contentLength())
                .body(resource);
    }

    @GetMapping("/downTemplate")
    public ResponseEntity<Resource> downFile(@RequestParam Integer templateId , String username) throws IOException {
// 构建文件路径
//        Path filePath = Paths.get(FILE_DIRECTORY);
//        Resource resource = new UrlResource(filePath.toUri());
        log.info("文件Id：{},用户名：{}", templateId, username);

//        boolean isNotSuperUser = resumeTemplateService.isNotSuperUser(username);
//        boolean isBuy = resumeTemplateService.buyTemplate(templateId, username);
//        if (!isNotSuperUser && !isBuy) {
//            log.error("非超级管理员或未购买该模板，无法下载");
//            return null;
//        } else {

            // 检查文件是否存在
//        if (!resource.exists()) {
//            throw new RuntimeException("文件不存在: ");
//        }

            ResumeTemplateDownAndPreviewVo resumeTemplateDownAndPreviewVo = resumeTemplateService.ByIdDownResumeTemplate(templateId);
            byte[] file = resumeTemplateDownAndPreviewVo.getDownFile();
            // 转换为Resource
            Resource resource = new ByteArrayResource(file);
            String fileName = resumeTemplateDownAndPreviewVo.getName();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\"");
            headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");

            return ResponseEntity.ok()
                    .headers(headers)
                    .contentLength(resource.contentLength())
                    .body(resource);
        }
//    }

}
